
//
//  AccessTokenItem.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/16.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit

class AccessTokenItem: NSObject,NSCoding {
    var access_token: String?
    var expires_in: NSTimeInterval = 0.0 {
        didSet {
        expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var uid: String?
    var expires_date: NSDate?
    var screen_name: String?
    var avatar_large: String?
    
    //字典转模型便利构造方法
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写setValue方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //重写计算属性
    override var description: String {
        get {
        return dictionaryWithValuesForKeys(["access_token", "expires_in", "uid", "expires_date", "screen_name", "avatar_large"]).description
        }
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
